# Look at the SETTINGS file in this directory; edit it.
# Do the same with SETTINGS.pl in the WebDB subdirectoty
# BIG NOTE: both should be unified!!!!!

# gmake install		   Compiles and installs everything
#
# gmake compile		   Compiles all source files
#
# gmake dump-conecta2amos  Dumps Conecta data stored in a
# 			   simplified database to the Amos database
#
# gmake dump-persdb2mysql  Dumps the data stored in the persdb
# 			   Amos database into the MySql Amos database
#
# gmake dump-mysql2persdb  Dumps the data stored in the MySql
# 			   Amos database into the persdb Amos database
#
# gmake uninstall 	   Removes the WWW pages.  



SOURCEDIR:=$(shell pwd)
CIAOALIAS="search=$(SOURCEDIR)/Prototype:portalfiles=$(SOURCEDIR)/PortalFiles:amos=$(SOURCEDIR):db=$(SOURCEDIR)/DataBase"

include SETTINGS

include COMMON

DEFAULTS_FILE=./configuration.pl
default:
	@echo Please state a target

## Calling set-up-dirs here is a VERY BAD IDEA.  However, WebDB creates a log
## file with a header when compiling.  Creating the directory
## infrastructure before should work around this. 

compile: gen-database-link \
	 generate-conf-files \
	 set-up-dirs \
	 compile-portal \
	 compile-search \
	 compile-fillin \
	 compile-webdb \
	 compile-dumps

install: compile just-install adjust-permissions gen-start-stop-script create-database-$(DATABASE_TYPE) drop-database-$(DATABASE_TYPE) gen-database-link

gen-database-link:
	cd DataBase &&  $(MAKE) gen-database-link-$(DATABASE_TYPE)

compile-dumps:
	cd DataBase && $(MAKE) SOURCEDIR=$(SOURCEDIR) CIAOALIAS=$(CIAOALIAS) compile-dumps

dump-conecta2amos:
	@echo "--------------------------------------------------------------"
	@echo " Dump of conecta database to Amos database"
	@echo "--------------------------------------------------------------"
	cd DataBase && $(MAKE) run-dump-conecta2amos
	$(MAKE) adjust-permissions

dump-persdb2mysql:
	@echo "--------------------------------------------------------------"
	@echo " Dump of persdb database to MySql database"
	@echo "--------------------------------------------------------------"
	cd DataBase && $(MAKE) run-dump-persdb2mysql
	$(MAKE) adjust-permissions

dump-mysql2persdb:
	@echo "--------------------------------------------------------------"
	@echo " Dump of mysql database to persdb database"
	@echo "--------------------------------------------------------------"
	cd DataBase && $(MAKE) run-dump-mysql2persdb
	$(MAKE) adjust-permissions

adjust-permissions:
	find $(INSTALLATION_ROOT) -type d -exec /bin/chmod $(WEB_DIR_PERMISSIONS) {} \;
	find $(INSTALLATION_ROOT) -name '.addr' -exec /bin/chmod $(WEB_FILE_PERMISSIONS) {} \;
	find $(INSTALLATION_ROOT) -name '*.html' -exec /bin/chmod $(WEB_FILE_PERMISSIONS) {} \;
	find $(INSTALLATION_ROOT) -name '*.cgi' -exec /bin/chmod $(WEB_CGI_PERMISSIONS) {} \;
	find $(INSTALLATION_ROOT) -name '*.gif' -exec /bin/chmod $(WEB_FILE_PERMISSIONS) {} \;
	find $(INSTALLATION_ROOT) -name '*.jpg' -exec /bin/chmod $(WEB_FILE_PERMISSIONS) {} \;
#
	find $(SAVE_DATA_DIR) -type d -exec /bin/chmod $(DATABASE_DIR_PERMISSIONS) {} \;
	find $(SAVE_DATA_DIR) -type d -exec /bin/chgrp $(DATABASE_DIR_GROUP) {} \;
	find $(SAVE_DATA_DIR) -name '*.pl' -exec /bin/chmod $(DATABASE_FILE_PERMISSIONS) {} \;
	find $(SAVE_DATA_DIR) -name '*.pl' -exec /bin/chgrp $(DATABASE_FILE_GROUP) {} \;
#
	find $(PERSISTENT_DATA_DIR) -type d -exec /bin/chmod $(DATABASE_DIR_PERMISSIONS) {} \;
	find $(PERSISTENT_DATA_DIR) -type d -exec /bin/chgrp $(DATABASE_DIR_GROUP) {} \;
	find $(PERSISTENT_DATA_DIR) -name '*.pl' -exec /bin/chmod $(DATABASE_FILE_PERMISSIONS) {} \;
	find $(PERSISTENT_DATA_DIR) -name '*.pl' -exec /bin/chgrp $(DATABASE_FILE_GROUP) {} \;
	find $(INSTALLATION_ROOT) -name '.htpasswd' -exec /bin/chmod $(WEB_FILE_PERMISSIONS) {} \;

clean: 
	find . -name '*.po' -exec rm {} \;
	find . -name '*.itf' -exec rm {} \;
	find . -name '*.asr' -exec rm {} \;
	cd PortalFiles && $(MAKE) SOURCEDIR=$(SOURCEDIR) clean
	cd Prototype && $(MAKE) SOURCEDIR=$(SOURCEDIR) clean 
	cd Insertion_form && $(MAKE) SOURCEDIR=$(SOURCEDIR) clean 
	cd DataBase && $(MAKE) SOURCEDIR=$(SOURCEDIR) clean 
	cd WebDB && $(LPMAKE) clean

distclean: clean
	find . -name '*~' -exec rm {} \; 
	-rm $(DB_CREATION_SCRIPT)
	-rm $(DB_USER_CREATION_FILE)
	-rm $(DB_REMOVAL_SCRIPT)
	-rm $(DB_REMOVAL_FILE)
	cd PortalFiles && $(MAKE) SOURCEDIR=$(SOURCEDIR) distclean 
	cd Prototype && $(MAKE) SOURCEDIR=$(SOURCEDIR) distclean 
	cd Insertion_form && $(MAKE) SOURCEDIR=$(SOURCEDIR) distclean
	cd DataBase && $(MAKE) SOURCEDIR=$(SOURCEDIR) distclean 
	cd WebDB && $(LPMAKE) distclean

# Uninstall removes the WWW pages.  Its contents are static in the
# sense that they do not change due to user interaction, and they can
# be regenerated from the sources at any time, so it is safe to just
# erase them.
uninstall: 
	rm -rf $(INSTALLATION_ROOT)

just-install:   just-install-pages \
		just-install-search \
		just-install-fillin \
		just-install-download-amosdb \
		just-install-webdb \
		just-install-webdb-admin-passwd	\
		adjust-permissions

gen-start-stop-script:
	@/bin/rm -f $(DBSERVER_START_SCRIPT) $(DBSERVER_STOP_SCRIPT)
	@echo '#!/bin/sh' > $(DBSERVER_START_SCRIPT)
	@echo '# This script has been automatically generated' >> $(DBSERVER_START_SCRIPT)
	@echo '# Do not edit it by hand!' >> $(DBSERVER_START_SCRIPT)
	@echo "cd $(INSTALLATION_ROOT)/$(WEBDBINSTANCENAME)/admin ; ./amos_start" >> $(DBSERVER_START_SCRIPT)
	@echo '#!/bin/sh' > $(DBSERVER_STOP_SCRIPT)
	@echo '# This script has been automatically generated' >> $(DBSERVER_STOP_SCRIPT)
	@echo '# Do not edit it by hand!' >> $(DBSERVER_STOP_SCRIPT)
	@echo "cd $(INSTALLATION_ROOT)/$(WEBDBINSTANCENAME)/admin ; ./amos_stop" >> $(DBSERVER_STOP_SCRIPT)
	@echo "/bin/rm -f $(ADDR_DIR)/.lock" >>  $(DBSERVER_STOP_SCRIPT)
	@chmod $(WEB_CGI_PERMISSIONS) $(DBSERVER_START_SCRIPT) $(DBSERVER_STOP_SCRIPT) 
	@echo
	@echo ============================================================
	@echo Please use the scripts $(DBSERVER_START_SCRIPT) 
	@echo and $(DBSERVER_STOP_SCRIPT) to start and stop the dbserver
	@echo ============================================================
	@echo


#---------------------------------------------------------------------------
compile-portal:
	cd PortalFiles && $(MAKE) SOURCEDIR=$(SOURCEDIR) CIAOALIAS=$(CIAOALIAS) compile 

compile-search: 
	cd Prototype &&  $(MAKE) SOURCEDIR=$(SOURCEDIR) CIAOALIAS=$(CIAOALIAS) compile 

compile-fillin: 
	cd Insertion_form &&  $(MAKE) SOURCEDIR=$(SOURCEDIR) CIAOALIAS=$(CIAOALIAS) compile 

compile-webdb:
	cd WebDB &&  CIAOALIASPATH=$(CIAOALIAS) $(LPMAKE) compile


#---------------------------------------------------------------------------

just-install-pages:
	cd PortalFiles && $(MAKE) SOURCEDIR=$(SOURCEDIR) just-install
	cd Images && $(MAKE) SOURCEDIR=$(SOURCEDIR) just-install

just-install-search:
	cd Prototype && $(MAKE) SOURCEDIR=$(SOURCEDIR) just-install

just-install-fillin:
	cd Insertion_form && $(MAKE) SOURCEDIR=$(SOURCEDIR) just-install

DOWNLOAD_AMOSDB_CGI=$(CGI_DIR)/download_amosdb.cgi

just-install-download-amosdb:
	@echo "Creating download-amosdb.cgi"
	@sh Prototype/download_amosdb_template $(SAVE_DATA_DIR) > $(DOWNLOAD_AMOSDB_CGI) 

just-install-webdb: 
	cd WebDB && CIAOALIASPATH=$(CIAOALIAS) $(LPMAKE) just_install

just-install-webdb-admin-passwd: generate-htaccess
	cd $(INSTALLATION_ROOT)$(WEBDBINSTANCENAME)/admin && \
	/usr/bin/htpasswd -cmb .htpasswd $(WWW_ADMIN_USER) $(WWW_ADMIN_PASSWORD)

HTACCESS_FILE=$(INSTALLATION_ROOT)$(WEBDBINSTANCENAME)/admin/.htaccess
HTPASSWD_FILE=$(INSTALLATION_ROOT)$(WEBDBINSTANCENAME)/admin/.htpasswd

generate-htaccess:
	@echo "AuthUserFile '$(HTPASSWD_FILE)'" > $(HTACCESS_FILE)
	@echo "AuthGroupFile /dev/null" >> $(HTACCESS_FILE)
	@echo "AuthName \"Amos Restricted Area\"" >> $(HTACCESS_FILE)
	@echo "AuthType Basic" >> $(HTACCESS_FILE)
	@echo "<Limit GET>" >> $(HTACCESS_FILE)
	@echo "require user '$(WWW_ADMIN_USER)'" >> $(HTACCESS_FILE)
	@echo "</Limit>" >> $(HTACCESS_FILE)

# install-search: generate-auto-files \
# 		compile-search \
# 		do-install-search
# 
# 
# install-search-test: generate-auto-files-test \
# 		     compile-search \
# 		     do-install-search-test

#---------------------------------------------------------------------------

generate-conf-files: clean-conf-files
	@echo "%% This file has been generated automatically" \
	 >  $(DEFAULTS_FILE)
	@echo "%% Do not change it by hand!  Edit instead the SETTINGS file." \
	 >>  $(DEFAULTS_FILE)
	@echo ":- module(_, _, [])."   >>  $(DEFAULTS_FILE)
	@echo "root_dir('$(INSTALLATION_ROOT)')." >> $(DEFAULTS_FILE)
	@echo "root_url('$(INSTALLATION_URL)')." >> $(DEFAULTS_FILE)
	@echo "amos_email('$(REVIEWER_EMAIL)')." >> $(DEFAULTS_FILE)
	@echo "css_dir('$(CSS_DIR)')." >> $(DEFAULTS_FILE)
	@echo "css_url('$(CSS_URL)')." >> $(DEFAULTS_FILE)
	@echo "cgi_dir('$(CGI_DIR)')." >> $(DEFAULTS_FILE)
	@echo "fillin_dir('$(DBFILLIN_DIR)')." >> $(DEFAULTS_FILE)
	@echo "cgi_url('$(CGI_URL)')." >> $(DEFAULTS_FILE)
	@echo "cgi_root('$(INSTALLATION_URL_CGI_ROOT)/$(SUBROOT_PATH)/')." >> $(DEFAULTS_FILE)
	@echo "images_dir('$(IMAGES_DIR)')." >> $(DEFAULTS_FILE)
	@echo "images_url('$(IMAGES_URL)')." >> $(DEFAULTS_FILE)
#       @echo "pers_data_perms('$(PERSISTENT_DATA_DIR)')." >> $(DEFAULTS_FILE)
	@echo "www_group($(WWW_GROUP))." >> $(DEFAULTS_FILE)
	@echo "interm_file_group($(INTERMEDIATE_FILE_GROUP))." >> $(DEFAULTS_FILE)
	@echo "interm_file_perms($(INTERMEDIATE_FILE_PERMISSIONS))." >> $(DEFAULTS_FILE)
	@echo "web_file_perms($(WEB_FILE_PERMISSIONS))." >> $(DEFAULTS_FILE)
	@echo "web_cgi_perms($(WEB_CGI_PERMISSIONS))." >> $(DEFAULTS_FILE)
	@echo "db_name('$(DATABASE_NAME)')." >> $(DEFAULTS_FILE)
	@echo "db_user('$(DATABASE_USER)')." >> $(DEFAULTS_FILE)
	@echo "db_password('$(DATABASE_PASSWORD)')." >> $(DEFAULTS_FILE)
	@echo "db_host('$(DATABASE_HOST)':$(DATABASE_PORT))." >>	$(DEFAULTS_FILE)
	@echo "db_dir_perms($(DATABASE_DIR_PERMISSIONS))." >> $(DEFAULTS_FILE)
	@echo "db_file_perms($(DATABASE_FILE_PERMISSIONS))." >> $(DEFAULTS_FILE)

	@echo "stat_pages('$(STATICPAGESGEN)')." >> $(DEFAULTS_FILE)
	@echo "webdb_templates('$(WEBDBTEMPLATESGEN)')." >> $(DEFAULTS_FILE)
	@echo "css_portal('$(CSSFILES)')." >> $(DEFAULTS_FILE)
	@echo "pers_data_dir('$(PERSISTENT_DATA_DIR)')." >> $(DEFAULTS_FILE)
	@echo "log_dir('$(LOG_DIR)')." >> $(DEFAULTS_FILE)
	@echo "save_data_dir('$(SAVE_DATA_DIR)')." >> $(DEFAULTS_FILE)
	@echo "add('$(ADDR_DIR)')." >> $(DEFAULTS_FILE)
	@echo "wdbn('$(WEBDBINSTANCENAME)')." >> $(DEFAULTS_FILE)
	@echo "wdbn_url('$(INSTALLATION_URL_CGI_ROOT)/$(SUBROOT_PATH)/$(WEBDBINSTANCENAME)')." >> $(DEFAULTS_FILE)
	@echo "sourcedir('$(SOURCEDIR)')." >> $(DEFAULTS_FILE)

generate-conf-files-test: clean-conf-files
	-rm -f $(DEFAULTS_FILE)
	@echo "%% This file has been generated automatically" \
	 >  $(DEFAULTS_FILE)
	@echo "%% Do not change it by hand!  Edit instead the SETTINGS file." \
	 >>  $(DEFAULTS_FILE)
	@echo ":- module(_, _, [])."   >>  $(DEFAULTS_FILE)
	@echo "css_dir(\"$(CSS_DIR_TEST)\")." >> $(DEFAULTS_FILE)
	@echo "css_url(\"$(CSS_URL_TEST)\")." >> $(DEFAULTS_FILE)
	@echo "cgi_dir(\"$(CGI_DIR_TEST)\")." >> $(DEFAULTS_FILE)
	@echo "cgi_url(\"$(CGI_URL_TEST)\")." >> $(DEFAULTS_FILE)
	@echo "fillin_dir('$(DBFILLIN_DIR_TEST)')." >> $(DEFAULTS_FILE)
	@echo "p_d_d('$(PERSISTENT_DATA_DIR_TEST)')." >> $(DEFAULTS_FILE)
	@echo "log_dir(\"$(LOG_DIR_TEST)\")." >> $(DEFAULTS_FILE)

create-database-persdb:
	@echo 

create-database-mysql:
	@echo "--------------------------------------------------------------"
	@echo "To create the Amos database user, root privileges are needed."
	@echo "Please, run the following script as database root:"
	@echo 
	@echo "  $(DB_CREATION_SCRIPT)"
	@echo 
	@echo "The operations performed on the database server are"
	@echo "located in the following files:"
	@echo "  $(DB_CREATION_FILE)"
	@echo "  $(DB_USER_CREATION_FILE)"
	@echo "--------------------------------------------------------------"
	@rm -f $(DB_CREATION_SCRIPT)
	@echo '#!/bin/sh' > $(DB_CREATION_SCRIPT)
	@echo "$(DATABASE_CLIENT_COMMAND) $(DATABASE_ROOT_FLAGS) < $(DB_USER_CREATION_FILE)" >> $(DB_CREATION_SCRIPT)
	@echo "$(DATABASE_CLIENT_COMMAND) $(DATABASE_CLIENT_FLAGS) $(DATABASE_NAME) < $(DB_CREATION_FILE)" >> $(DB_CREATION_SCRIPT)
	@/bin/chmod u+x $(DB_CREATION_SCRIPT)
	-rm -f $(DB_USER_CREATION_FILE)
	@echo "# DO NOT MODIFY THIS FILE. IT HAS BEEN GENERATED AUTOMATICALLY." > $(DB_USER_CREATION_FILE)
	@echo "# --------------------------------------------------------------" >> $(DB_USER_CREATION_FILE)
	@echo "# Database user creation script" >> $(DB_USER_CREATION_FILE)
	@echo "# --------------------------------------------------------------" >> $(DB_USER_CREATION_FILE)
	@echo "# This MySql script creates the database user which will be" >> $(DB_USER_CREATION_FILE)
	@echo "# used to create and use the AMOS database." >> $(DB_USER_CREATION_FILE)
	@echo "# This script *must* be run from a MySQL user with enough" >> $(DB_USER_CREATION_FILE)
	@echo "# privileges to do user maintenance (such as root)." >> $(DB_USER_CREATION_FILE)
	@echo "# --------------------------------------------------------------" >> $(DB_USER_CREATION_FILE)
	@echo "create database $(DATABASE_NAME);" >> $(DB_USER_CREATION_FILE)
	@echo "use $(DATABASE_NAME);" >> $(DB_USER_CREATION_FILE)
	@echo "grant create, delete, select, update, insert" >> $(DB_USER_CREATION_FILE)
	@echo " on $(DATABASE_NAME).*" >> $(DB_USER_CREATION_FILE)
	@echo "	to '$(DATABASE_USER)'@'$(DATABASE_CLIENT_HOST)'" >> $(DB_USER_CREATION_FILE)
	@echo "	identified by '$(DATABASE_PASSWORD)';" >> $(DB_USER_CREATION_FILE)
	@echo "grant create, delete, select, update, insert" >> $(DB_USER_CREATION_FILE)
	@echo " on $(DATABASE_NAME).*" >> $(DB_USER_CREATION_FILE)
	@echo "	to '$(DATABASE_USER)'@'$(DATABASE_HOST)'" >> $(DB_USER_CREATION_FILE)
	@echo "	identified by '$(DATABASE_PASSWORD)';" >> $(DB_USER_CREATION_FILE)
	@echo "flush privileges;" >> $(DB_USER_CREATION_FILE)

# This rule *must* be executed *after* the database user is
# created with create-database rule.
# create-database:
# 	$(DATABASE_CLIENT_COMMAND) $(DATABASE_CLIENT_FLAGS) $(DATABASE_NAME) < DataBase/mysql_creation.sql

drop-database-persdb:
	@echo 

drop-database-mysql:
	@echo "--------------------------------------------------------------"
	@echo "To remove all the changes made to mysql by the AMOS setup,"
	@echo "root privileges are needed. To uninstall completely the AMOS"
	@echo "system, please run the following script as database root:"
	@echo 
	@echo "  $(DB_REMOVAL_SCRIPT)"
	@echo 
	@echo "The operations performed on the database server are"
	@echo "located in $(DB_REMOVAL_FILE)"
	@echo "--------------------------------------------------------------"
	@rm -f $(DB_REMOVAL_SCRIPT)
	@echo '#!/bin/sh' > $(DB_REMOVAL_SCRIPT)
	@echo '# Do not edit this file!' >> $(DB_REMOVAL_SCRIPT)
	@echo '# It has been generated automatically' >> $(DB_REMOVAL_SCRIPT)
	@echo "$(DATABASE_CLIENT_COMMAND) $(DATABASE_ROOT_FLAGS) < $(DB_REMOVAL_FILE)" >> $(DB_REMOVAL_SCRIPT)
	@/bin/chmod u+x $(DB_REMOVAL_SCRIPT)
	@rm -f $(DB_REMOVAL_FILE)
	@echo "# DO NOT MODIFY THIS FILE. IT HAS BEEN GENERATED AUTOMATICALLY." > $(DB_REMOVAL_FILE)
	@echo "# --------------------------------------------------------------" >> $(DB_REMOVAL_FILE)
	@echo "# Database removal script" >> $(DB_REMOVAL_FILE)
	@echo "# --------------------------------------------------------------" >> $(DB_REMOVAL_FILE)
	@echo "# This MySql script erases the database used" >> $(DB_REMOVAL_FILE)
	@echo "# by the AMOS system as well as the AMOS database user." >> $(DB_REMOVAL_FILE)
	@echo "# This script *must* be run from a MySQL user with enough" >> $(DB_REMOVAL_FILE)
	@echo "# privileges to do user maintenance (such as root)." >> $(DB_REMOVAL_FILE)
	@echo "# --------------------------------------------------------------" >> $(DB_REMOVAL_FILE)
	@echo "drop database $(DATABASE_NAME);" >> $(DB_REMOVAL_FILE)
	@echo "revoke all privileges on *.* from '$(DATABASE_USER)'@'$(DATABASE_CLIENT_HOST)';" >> $(DB_REMOVAL_FILE)
	@echo "revoke all privileges on *.* from '$(DATABASE_USER)'@'$(DATABASE_HOST)';" >> $(DB_REMOVAL_FILE)
	@echo "flush privileges;" >> $(DB_REMOVAL_FILE)

clean-conf-files:
	-rm -f $(DEFAULTS_FILE)

set-up-dirs:
	-/usr/bin/install -d --mode=$(INTERMEDIATE_FILE_PERMISSIONS) \
			--group=$(INTERMEDIATE_FILE_GROUP) \
			$(PERSISTENT_DATA_DIR) $(LOG_DIR) $(SAVE_DATA_DIR)

tags:
	-etags -l prolog Prototype/*.pl PortalFiles/*.pl DataBase/*.pl
